//
//  TopViewController.m
//  JWMovie
//
//  Created by Jorn.Wu on 15/7/17.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "TopViewController.h"
#import "JWTopMovieModel.h"
#import "JWTopMovieCell.h"
#import "constant.h"
#import "DetailViewController.h"
#import "RootBarViewController.h"

@interface TopViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UINavigationControllerDelegate>

@property (nonatomic, strong) NSArray *topMovieAr;
@property (nonatomic, strong) JWTopMovieModel *topModel;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSDictionary *jsonDic;

@end

@implementation TopViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"TOP250";
        self.navigationController.navigationBar.translucent = NO;
       
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.delegate = self;
    self.jsonDic = [self webRequestWithURLName:@"http://api.douban.com/v2/movie/top250"];
    [self loadColleCtionView];
    
    [self.collectionView registerClass:[JWTopMovieCell class] forCellWithReuseIdentifier:@"topMovieCell"];
}

- (NSArray *)topMovieAr{

    NSArray *listAr = [self.jsonDic objectForKey:@"subjects"];
    
    if (!_topMovieAr) {
        NSMutableArray *tempAr = [NSMutableArray array];
        for (int i = 0; i < listAr.count; i++) {
            NSDictionary *topMovieInfoDic = listAr[i];
            JWTopMovieModel *model = [JWTopMovieModel topModeMovielWithDictionary:topMovieInfoDic];
            [tempAr addObject:model];
        }
        _topMovieAr = tempAr;
    }
    
    return _topMovieAr;
   
}

- (void)loadColleCtionView{
    
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向
    flowLayout.minimumLineSpacing = 5.0;//行间距(最小值)
    flowLayout.minimumInteritemSpacing = 5.0;//item间距(最小值)
    flowLayout.itemSize = CGSizeMake(30, 30);//item的大小
    flowLayout.sectionInset = UIEdgeInsetsMake(7, 10, 7, 10);//设置section的边距
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, JWScreenWidth, JWScreenHeight - 64) collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor clearColor];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.topMovieAr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    JWTopMovieCell *cell = [JWTopMovieCell topMovieCellWithCollectionView:collectionView reuseIdentifier:@"topMovieCell" forIndexPath:indexPath];
    JWTopMovieModel *model = self.topMovieAr[indexPath.row];
    cell.topMovieModel = model;
    
    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat cellW = (JWScreenWidth - 10 * 4) / 3.f;
    CGSize size = CGSizeMake(cellW, 150);
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    RootBarViewController *tabBC = (RootBarViewController *)self.navigationController.parentViewController;
    
    if (navigationController.viewControllers.count == 1) {
        tabBC.tabBar.hidden = NO;
    }else {
        tabBC.tabBar.hidden = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
